#include"searcher.hpp"
#include "cpp-httplib/httplib.h"

const std::string input = "data/raw_html/raw.txt";
const std::string root_path = "./wwwroot";

int main() 
{
    ns_searcher::Searcher search;
    search.InitSearcher(input);

    httplib::Server svr;
    // 设置根目录
    svr.set_base_dir(root_path.c_str());
    svr.Get("/s", [&search](const httplib::Request &req, httplib::Response& res){
        // 提参
        if(!req.has_param("word"))
        {
            res.set_content("必须要有搜索关键字", "text/plain; charset=utf-8");
            return;
        }
        std::string word = req.get_param_value("word");
        // std::cout << "用户在搜索：" << word << std::endl;
        LOG(NORMAL, "用户在搜索： " + word);

        // 关键字提上来了，就要给用户提供搜索服务
        std::string json_string;
        search.Seacher(word, &json_string);

        // 搜索到了，给用户返回
        res.set_content(json_string.c_str(), "application/json");
        // res.set_content("hello world", "text/plain; charset=utf-8");
    });
    LOG(NORMAL, "服务器启动成功...");
    svr.listen("0.0.0.0", 8889);
    return 0;
}